<template>
  <div>
    <common-head title="确认订单" />
    <div class="wrap">
      <van-contact-card
        :type="cardType"
        :name="contact.linkMan"
        :tel="contact.mobile"
        @click="onEdit"
      />
      <div class="items">
        <div class="item" v-for="item in items" :key="item.id">
          <van-card
            :num="item.number"
            :price="item.price"
            :title="item.name"
            :thumb="item.pic"
          />
        </div>
      </div>
    </div>
    <van-submit-bar
      :price="total * 100"
      button-text="提交订单"
      @submit="onSubmit"
    />
  </div>
</template>

<script>
import CommonHead from 'components/CommonHead.vue'
import { fetchDefaultContact, fetchContactInfo, fetchCartItems } from 'api'
import { getToken } from 'utils'
export default {
  data () {
    return {
      contact: {},
      items: [],
      total: 0
    }
  },
  created () {
    this.fetchItems()
    this.fetchDefaultContact()
  },
  methods: {
    onEdit () {
      // 修改或者添加联系人
      this.$router.push('/contactlists')
    },
    fetchDefaultContact () {
      console.log(this.$route.query.id)
      if (this.$route.query.id) {
        fetchContactInfo({
          id: this.$route.query.id,
          token: getToken()
        }).then(res => {
          if (res.data.code === 0) {
            this.contact = res.data.data.info
          }
        })
      } else {
        fetchDefaultContact({
          token: getToken()
        }).then(res => {
          if (res.data.code === 0) {
            this.contact = res.data.data.info
          }
        })
      }
    },
    onSubmit () {},
    // 获取订单商品
    fetchItems () {
      fetchCartItems({
        token: getToken()
      }).then(res => {
        this.items = res.data.data.items.filter(item => item.selected)
        this.total = res.data.data.price
      })
    }
  },
  computed: {
    cardType () {
      return this.contact.id ? 'edit' : 'add'
    }
  },
  components: {
    CommonHead
  }
}
</script>

<style lang="scss" scoped></style>
